package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.INewsService;
import com.cn.wanxi.util.IOTool;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.Tool;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NewsServiceImpl implements INewsService {
    @Override
    public Object getNewsModelsForHomePage() {
//        String path = "E:\\wanxi\\step-3\\canyin03-0615\\web\\note";
//        String fileName = "newsForHomePage.txt";
//        1.创建文件
        IOTool.createNote(Path.PATH, Path.FILE_NAME_NEWS_FOR_HOME_PAGE);
//        2.写入数据
        List<String> data = setNavData();
        IOTool.writeData(data);
//        3.读取
        String string = IOTool.readData();
//        4封装
        return setData(string);
    }

    private Object setData(String string) {
        String[] split = string.split(IOTool.SPLIT);
        String title = Tool.replace(split[0]);
        String content = Tool.replace(split[1]);
        String[] titles = title.split(",");
        String[] contents = content.split(",");
        List<NewsModel> list = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            NewsModel newsModel = new NewsModel();
            newsModel.setTitle(titles[i]);
            newsModel.setContent(contents[i]);
            list.add(newsModel);
        }
        return list;
    }

    private List<String> setNavData() {
        NewsDao newsDao = new NewsDao();
        List<String> list = new ArrayList<>();
        list.add(Arrays.toString(newsDao.titlesForHomePage));
        list.add(Arrays.toString(newsDao.contentsForHomePage));
        return list;
    }
}
